<?php

namespace App\Controller\App;

use App\Base\BaseConfig;
use App\Base\BaseController4Restful;
use App\Entity\UserArticle;
use App\Repository\ArticleRepository;
use App\Repository\CommentRepository;
use App\Repository\PageRepository;
use App\Repository\UserArticleRepository;
use App\Service\MoneyService;
use Symfony\Component\Routing\Annotation\Route;
use function Doctrine\ORM\QueryBuilder;

class ArticleController extends BaseController4Restful
{
    /**
     * @Route("/app/art/list", name="AppGetArticleList")
     */
    public function AppGetArticleList(ArticleRepository $repository)
    {
        if(!$cat = $this->getParamRequired("cat")){$cat = 0;}
        if(!$page = $this->getParamRequired("page")){$page = 1;}
        $data = [];
        $qb = $repository->createQueryBuilder("A");
        $qb->join("A.cat","Cat");

        if(!$cats = $this->getParamRequired("cats")){$cats = "";}
        if ($cats) {
            if (!is_array($cats)) {
                $cats = json_decode($cats,true);
            }
            $qb->where(
                $qb->expr()->orX(
                    $qb->expr()->in("A.cat",$cats),
                    $qb->expr()->in("Cat.parent",$cats)
                )
            );
        }

        if ($cat) {
            $qb->where(
                $qb->expr()->orX(
                    $qb->expr()->eq("A.cat",$cat),
                    $qb->expr()->eq("Cat.parent",$cat)
                )
            );
        }


        $orderBy = $this->getParamRequired("orderBy");
        switch ($orderBy) {
            case "view":
                $qb->orderBy("A.view","DESC");
                break;
            case "comment":
                $qb->orderBy("A.comment","DESC");
                break;
            default:
                $qb->orderBy("A.createAt","DESC");
        }

        $qb->setMaxResults(10);
        $qb->setFirstResult($page * 10 - 10 );

        $data = $qb->getQuery()->execute();
        return $this->json_out(200,"数据获取成功",$data);
    }


    /**
     * @Route("/app/art/info", name="AppGetArticleInfo")
     */
    public function AppGetArticleInfo(ArticleRepository $articleRepository,CommentRepository $commentRepository,UserArticleRepository $userArticleRepository)
    {
        if(!$id = $this->getParamRequired("id")){return $this->json_out(400,"参数缺失");}
        $art = $articleRepository->find($id);
        if (!$art) {
            return $this->json_out(404,"内容未找到");
        }
        //更新用户查看数据
        $art->setView((int)$art->getView() + 1);
        $this->myEm()->persist($art);
        $this->myEm()->flush();


        $user = $this->needToBeUser();
        //文章内容
        //检查文章是否已经购买
        $data["canRead"] = $userArticleRepository->checkIsBuy($art,$user);
        //文章评论
        $data["comment"] = $commentRepository->getByWhat($id);
        $art = $this->toArray($art);
        $art["content"] = $this->dealWithImgInHtml($art["content"]);
        $data["art"] = $art;
        return $this->json_out(200,"数据获取成功",$data);
    }

    /**
     * @Route("/app/art/pay", name="AppPayForArticleInfo")
     */
    public function AppPayForArticleInfo(ArticleRepository $articleRepository,MoneyService $moneyService,UserArticleRepository $userArticleRepository)
    {
        $user = $this->needToBeUser();
        if(!$id = $this->getParamRequired("id")){return $this->json_out(400,"参数缺失");}
        $art = $articleRepository->find($id);
        if (!$art) {
            return $this->json_out(404,"内容未找到");
        }
        if ($userArticleRepository->checkIsBuy($art,$user)) {
            return $this->json_out(400,"已购买");
        }
        //购买文章
        //---检查资金是否充足
        $need = $art->getPrice();
        $userMoney = $moneyService->getUserMoney($user);
        if ($userMoney["jifen"] < $need) {
            return $this->json_out(400,"您没有足够的积分");
        }
        //---添加文章到自己的库
        $UserArticle = new UserArticle();
        $UserArticle->setCreateAt(new \DateTime())
            ->setUser($user)
            ->setArticle($art)
            ->setPrice($need);
        $this->myEm()->persist($UserArticle);$this->myEm()->flush();
        //---扣除费用
        $moneyService->mod($user,$need*-1,MoneyService::__MONEY_JI_FEN__,210,"赞助获取",["artId"=>$art->getId() ]);

        return $this->json_out(200,"购买成功",$art);
    }

}
